package com.happycat.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.happycat.base.R;
import com.happycat.base.exception.RestApiException;
import com.happycat.mapper.social.SocialVoiceRoomMemberMapper;
import com.happycat.mobile.model.UserAccount;
import com.happycat.mobile.service.UserAccountService;
import com.happycat.social.dto.SocialVoiceRoomMemberSaveDto;
import com.happycat.social.entity.SocialVoiceRoom;
import com.happycat.social.entity.SocialVoiceRoomMember;
import com.happycat.social.service.ISocialVoiceRoomMemberService;
import com.happycat.social.service.ISocialVoiceRoomPermissionService;
import com.happycat.social.service.ISocialVoiceRoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 *
 * @author jsonbin
 * @date 2020-09-10
 */
@Service
public class SocialVoiceRoomMemberServiceImpl extends ServiceImpl<SocialVoiceRoomMemberMapper, SocialVoiceRoomMember> implements ISocialVoiceRoomMemberService {
    @Autowired
    private ISocialVoiceRoomService socialVoiceRoomService;
    @Autowired
    private UserAccountService userAccountService;

    @Autowired
    private ISocialVoiceRoomPermissionService socialVoiceRoomPermissionService;

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public SocialVoiceRoomMember add(SocialVoiceRoomMemberSaveDto socialVoiceRoomMemberSaveDto) {
        //查询是否存在对应名单
        SocialVoiceRoomMember ws = new SocialVoiceRoomMember();
        ws.setAccountId(socialVoiceRoomMemberSaveDto.getAccountId());
        ws.setSvrId(socialVoiceRoomMemberSaveDto.getSvrId());
        ws.setType(socialVoiceRoomMemberSaveDto.getType());
        SocialVoiceRoomMember member = getOne(new QueryWrapper<>(ws));
        if(member!=null){
            throw new RestApiException(R.fail("已存在该名单，请勿重复添加"));
        }
        //查询房间并校验是否可用
        SocialVoiceRoom room = socialVoiceRoomService.checkAndGetById(socialVoiceRoomMemberSaveDto.getSvrId());
        //查询用户并校验是否存在或可用
        UserAccount userAccount = userAccountService.checkUserById(socialVoiceRoomMemberSaveDto.getAccountId());

        //校验权限
        if(socialVoiceRoomMemberSaveDto.getType().intValue() == SocialVoiceRoomMember.MANAGER_MEMBER){
            if(!socialVoiceRoomPermissionService.hasRoomCreatorPermission(room)){
                throw new RestApiException(R.fail("无权操作"));
            }
        }else {
            if(!socialVoiceRoomPermissionService.hasRoomManagerPermission(room)){
                throw new RestApiException(R.fail("无权操作"));
            }
        }

        member = BeanUtil.copyProperties(socialVoiceRoomMemberSaveDto, SocialVoiceRoomMember.class);
        member.setUserId(userAccount.getImUserId());
        member.setRoomId(room.getRoomId());
        if(save(member)){
            return member;
        }else {
            throw new RestApiException(R.fail("新增失败"));
        }
    }

    @Override
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Boolean del(String ids) {
        if(StrUtil.isBlank(ids)){
            throw new RestApiException(R.fail("参数为空"));
        }
        SocialVoiceRoomMember member = getById(ids.split(",")[0]);
        //查询房间并校验是否可用
        SocialVoiceRoom room = socialVoiceRoomService.checkAndGetById(member.getSvrId());
        if(!socialVoiceRoomPermissionService.hasRoomManagerPermission(room)){
            throw new RestApiException(R.fail("无权操作"));
        }
        Arrays.stream(ids.split(",")).forEach(id -> {
            if(!removeById(id)){
                throw new RestApiException(R.fail("取消名单失败"));
            }
        });
        return true;
    }

    @Override
    public Boolean check(Long svrId, int type, Long accountId) {
        socialVoiceRoomService.checkAndGetById(svrId);
        //查询是否存在对应名单
        SocialVoiceRoomMember ws = new SocialVoiceRoomMember();
        ws.setAccountId(accountId);
        ws.setSvrId(svrId);
        ws.setType(type);
        SocialVoiceRoomMember member = getOne(new QueryWrapper<>(ws));
        if(member==null){
            return false;
        }
        return true;
    }

}
